package com.test.pudamoney.view_pudamoney.info_input_pudamoney;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import static com.test.pudamoney.constant_pudamoney.Constant_pudamoney.Data_Action_Phone;

import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.test.pudamoney.R;
import com.test.pudamoney.bean_pudamoney.process_pudamoney.ProcessData_pudamoney;
import com.test.pudamoney.util_pudamoney.StringUtil_pudamoney;
import com.test.pudamoney.view_pudamoney.base_pudamoney.BaseInfoInputViewManager_pudamoney;
import com.test.pudamoney.view_pudamoney.base_pudamoney.OnInputChangeListener_pudamoney;

public class InputViewManager_pudamoney extends BaseInfoInputViewManager_pudamoney {

    private TextView titleTv;
    private EditText inputEt;

    private OnInputChangeListener_pudamoney onInputChangeListener;

    public InputViewManager_pudamoney(View view, ProcessData_pudamoney data){
        super(view,data);
    }

    @Override
    protected void init(){

        titleTv = mView.findViewById(R.id.view_input_tv_title);
        inputEt = mView.findViewById(R.id.view_input_et_input);

        setTitle(mData.getTitle());
        inputEt.setHint(StringUtil_pudamoney.getSafeString(mData.getHint()));

        //联系人电话输入数字
        if (mData.getAction().equalsIgnoreCase(Data_Action_Phone)) inputEt.setInputType(InputType.TYPE_CLASS_NUMBER);

        inputEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                if (onInputChangeListener !=null) onInputChangeListener.onInputChange(s.toString());
            }
        });
    }


    @Override
    public boolean checkInput() {
        if (!mData.isMustInput()) return true;
        return !TextUtils.isEmpty(inputEt.getText());
    }

    @Override
    public String getInput() {
        return StringUtil_pudamoney.getSafeString(inputEt.getText().toString());
    }

    public void addTextChangeListener(TextWatcher textWatcher){
        inputEt.addTextChangedListener(textWatcher);
    }

    public void setTitle(String title){
        if (TextUtils.isEmpty(title)) titleTv.setVisibility(GONE);
        else {
            titleTv.setVisibility(VISIBLE);
            titleTv.setText(title);
        }
    }

    public void setOnInputChangeListener(OnInputChangeListener_pudamoney onInputChangeListener) {
        this.onInputChangeListener = onInputChangeListener;
    }
}
